package com.demo.springBoot2.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.demo.springBoot2.entity.dto.MyFriendAndInfoDTO;
import com.demo.springBoot2.entity.dto.UsersInfoLoginDTO;
import com.demo.springBoot2.entity.vo.FriendReviewVO;
import com.demo.springBoot2.entity.vo.UsersFriendRegisterVO;
import com.demo.springBoot2.result.MyPage;
import com.demo.springBoot2.result.Result;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import com.demo.springBoot2.service.IUsersFriendService;
import com.demo.springBoot2.entity.UsersFriend;
import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 * 好友关系 前端控制器
 * </p>
 *
 * @author wuHua·pl
 * @since 2024-03-16
 */
@Api(tags = "好友关系")
@RestController
@RequestMapping("/users-friend")
public class UsersFriendController {

    @Resource
    private IUsersFriendService usersFriendService;

    /**
     * 查询所有接口
     *
     * @return
     */
    @ApiOperationSupport(author = "wuHua·pl")
    @ApiOperation(value = "查询所有接口", protocols = "查询好友关系所有数据")
    @GetMapping
    public Result<List<UsersFriend>> findAll() {
        return Result.success(usersFriendService.list()).msg("查询所有成功");
    }

    /**
     * 根据id查询数据接口
     *
     * @param id
     * @return
     */
    @ApiOperationSupport(author = "wuHua·pl")
    @ApiOperation(value = "根据id查询数据接口", protocols = "通过组件id查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "组件id", required = true)
    })
    @GetMapping("/{id}")
    public Result<UsersFriend> findOne(@PathVariable Integer id) {
        return Result.success(usersFriendService.getById(id)).msg("根据ID查询成功");
    }

    /**
     * 分页查询接口
     *
     * @param pageNum
     * @param pageSize
     * @return
     */
    @ApiOperationSupport(author = "wuHua·pl")
    @ApiOperation(value = "分页查询接口", protocols = "分页查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "页码", example = "1", required = true),
            @ApiImplicitParam(name = "pageSize", value = "每页多少条", example = "10", required = true)
    })
    @GetMapping("/page")
    public Result<MyPage<UsersFriend>> findPage(@RequestParam Integer pageNum, @RequestParam Integer pageSize) {
        QueryWrapper<UsersFriend> queryWrapper = new QueryWrapper<>();
        return Result.success().data(usersFriendService.page(new Page<>(pageNum, pageSize), queryWrapper)).msg("分页查询成功");
    }

    /**
     * 新增接口
     *
     * @param
     * @return
     */
    @ApiOperationSupport(author = "wuHua·pl")
    @ApiOperation(value = "新增接口")
    @PostMapping("/add/{friendId}")
    public Result<String> add(@PathVariable Integer friendId) {
        Integer userId = StpUtil.getLoginIdAsInt();
        UsersFriendRegisterVO usersFriendRegisterVO = new UsersFriendRegisterVO(userId, friendId);
        UsersFriend usersFriend = new UsersFriend();
        BeanUtil.copyProperties(usersFriendRegisterVO, usersFriend);
        usersFriendService.save(usersFriend);
        return Result.success().msg("新增成功");
    }

    /**
     * 更新接口
     *
     * @param
     * @return
     */
    @ApiOperationSupport(author = "wuHua·pl")
    @ApiOperation(value = "更新接口")
    @PutMapping("/update")
    public Result<String> save(@RequestBody UsersFriend usersFriend) {
        UsersFriend usersFriend1 = usersFriendService.getById(usersFriend.getId());
        if (usersFriend1 != null){
            usersFriend1.setStatus(usersFriend.getStatus());
            usersFriendService.saveOrUpdate(usersFriend);
            return Result.success().msg("更新成功");
        }
        return Result.error().msg("更新失败");
    }

    /**
     * 删除接口
     *
     * @param id
     * @return
     */
    @ApiOperationSupport(author = "wuHua·pl")
    @ApiOperation(value = "删除接口", protocols = "通过id删除")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "组件id", required = true)
    })
    @DeleteMapping("/{id}")
    public Result<String> delete(@PathVariable Integer id) {
        usersFriendService.removeById(id);
        return Result.success().msg("批量删除成功");
    }

    /**
     * 批量删除接口
     *
     * @param ids
     * @return
     */
    @ApiOperationSupport(author = "wuHua·pl")
    @ApiOperation(value = "批量删除接口", protocols = "批量删除接口")
    @PostMapping("/del/batch")
    public Result<String> deleteBatch(@RequestBody List<Integer> ids) {
        usersFriendService.removeByIds(ids);
        return Result.success();
    }
    /**
     * 好友申请列表
     * */

    @ApiOperationSupport(author = "Mr.Abel")
    @ApiOperation(value = "根据ID查询用户的好友申请列表",protocols = "查询申请")
    @GetMapping("/getFriendByUserId")
    public Result<UsersInfoLoginDTO> getFriendByUserId() {
        Integer userId = StpUtil.getLoginIdAsInt();
        List<MyFriendAndInfoDTO> usersFriendList = usersFriendService.getUsersFriendByUserId(userId);
        return Result.success(usersFriendList).msg("查询成功");
    }
    /**
     * 好友列表
     * */

    @ApiOperationSupport(author = "Mr.Abel")
    @ApiOperation(value = "根据ID查询用户的好友列表",protocols = "查询好友")
    @GetMapping("/findFriendByUserId")
    public Result<UsersInfoLoginDTO> getFriendsByUserId() {
        Integer userId = StpUtil.getLoginIdAsInt();
        List<MyFriendAndInfoDTO> usersFriendList = usersFriendService.getFriendByUserId(userId);
        System.err.println("好友列表"+usersFriendList);
        return Result.success(usersFriendList).msg("查询成功");
    }
    /**
     * 好友审核
     *
     * @param friendReviewVO 包含好友请求信息的VO对象
     * @return 返回操作结果
     */
    @ApiOperationSupport(author = "Mr.Abel")
    @ApiOperation(value = "好友审核")
//    @SaCheckLogin // 检查用户登录状态
    @PostMapping("/friendReview")
    public Result friendReview(@RequestBody FriendReviewVO friendReviewVO) {
        // 获取当前登录用户的ID
        int loginIdAsInt = StpUtil.getLoginIdAsInt();
        // 创建UsersFriend对象并复制friendReviewVO的属性
        UsersFriend usersFriend = new UsersFriend();
        BeanUtil.copyProperties(friendReviewVO, usersFriend);
        // 设置当前登录用户为好友关系的对方ID
        usersFriend.setFriendId(loginIdAsInt);
        // 更新好友关系
        usersFriendService.updateById(usersFriend);
        // 创建反向的好友关系对象
        UsersFriend usersFriend1 = new UsersFriend();
        usersFriend1.setUserId(loginIdAsInt); // 设置当前登录用户为发起方
        // 根据传入的ID查询用户ID
        Integer userId = usersFriendService.getById(friendReviewVO.getId()).getUserId();
        // 设置反向好友关系的对方ID
        usersFriend1.setFriendId(userId);
        // 设置状态为已确认的好友关系
        usersFriend1.setStatus(1);
        // 保存反向的好友关系
        usersFriendService.save(usersFriend1);
        // 返回操作成功的Result对象
        return Result.success().msg("操作成功");
    }
}